package com.landiao.ctrler.management;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.landiao.common.factory.ResultMsgFactory;
import com.landiao.common.pojo.Page;
import com.landiao.common.pojo.Querier;
import com.landiao.common.pojo.ResultMsg;
import com.landiao.common.utils.CodecUtils;
import com.landiao.common.utils.PinyinUtil;
import com.landiao.ctrler.BaseCRUDCtrler;
import com.landiao.entities.Appointment;
import com.landiao.entities.Employee;
import com.landiao.entities.Holiday;
import com.landiao.entities.OrderSchedule;
import com.landiao.entities.Works;
import com.landiao.service.AppointmentService;
import com.landiao.service.EmployeeService;
import com.landiao.service.HolidayService;
import com.landiao.service.OrderScheduleService;
import com.landiao.service.WorksService;

@Controller
@RequestMapping("/management/employee")
public class EmployeeCtrler extends BaseCRUDCtrler<Employee, String>  {
	@Autowired
	private WorksService worksService;
	@Autowired
	private AppointmentService appointmentService;
	@Autowired
	private HolidayService holidayService;
	@Autowired
	private OrderScheduleService scheService;
	private EmployeeService getEmployeeService() {
        return (EmployeeService) baseService;
    }
	@Override
	public String save(Employee t, RedirectAttributes redirectAttr) {
		// TODO Auto-generated method stub
		t.setPasswd(CodecUtils.password(PinyinUtil.getFullSpell(t.getName())));
		t.setEmployeeId(CodecUtils.getUUID());
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setState("10A");
		t.setStatedate(new Date());
		baseService.save(t);
		redirectAttr.addFlashAttribute("resultMsg",
				ResultMsgFactory.success("添加成功,初始密码为：姓名全拼"));
		return redirectToUrl(viewName("index"));
	}
	
	@Override
	public Page<Employee> page(Employee t, Querier querier) {
		t.setCompId(this.getClientUser().getUser().getCompId());
		t.setShopId(this.getClientUser().getUser().getShopId());
		Page<Employee> page = getEmployeeService().queryByPage(querier,t);
		return page;
	}
	/**
	 * 验证账号是否存在
	 * 
	 */
	@RequestMapping("isExist")
	@ResponseBody
	public boolean isExist(String account,String id) {
		Employee user = this.getEmployeeService().getByAccount(account);
		if (user == null) {
			return true;
		}else{
			if(user.getEmployeeId().equals(id)){
				return true;
			}else{
				return false;
			}
		}
	}
	
	/**
	 * 验证账号是否存在
	 * 
	 */
	@RequestMapping(value = "resetPasswd/{id}", method = RequestMethod.POST)
	@ResponseBody
	public ResultMsg resetPasswd(@PathVariable String id) {
		Employee user = this.getEmployeeService().getById(id);
		user.setPasswd(CodecUtils.password("123456"));
		this.getEmployeeService().update(user);
		return ResultMsgFactory.success("重置密码成功，密码为：123456");
	}
	
    @RequestMapping("targetindex")
	public String targetindex(Model model) {
		return viewName("targetindex");
	}
	@RequestMapping(value = "targetedit/{id}", method = RequestMethod.GET)
	public String targetedit(Model model,@PathVariable String id) {
		Employee t = this.getEmployeeService().getById(id);
		model.addAttribute("t",t);
		return viewName("targetedit");
	}
	@RequestMapping(value = "targetupdate")
	public String targetupdate(Employee t,RedirectAttributes redirectAttr) {
		try{
			getEmployeeService().updateByPrimaryKeySelective(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl(viewName("targetindex"));
	}
    @RequestMapping("worksindex")
	public String worksindex(Model model) {
		return viewName("worksindex");
	}
	@RequestMapping(value = "holidaymanagement/{id}", method = RequestMethod.GET)
	public String holidaymanagement(Model model,@PathVariable String id) {
		Employee t = this.getEmployeeService().getById(id);
		//根据员工id查询休息日
		List<Holiday> holidaylist = holidayService.getHolidayListByEmployeeId(id);
		model.addAttribute("holidaylist",holidaylist);
		model.addAttribute("t",t);
		return viewName("holidaymanagement");
	}
	@RequestMapping(value = "worksmanagement/{id}", method = RequestMethod.GET)
	public String worksmanagement(Model model,@PathVariable String id) {
		Employee t = this.getEmployeeService().getById(id);
		//根据员工id查询作品库
		List<Works> workslist = worksService.getWorksListByEmployeeId(id);
		model.addAttribute("workslist",workslist);
		model.addAttribute("t",t);
		return viewName("worksmanagement");
	}
	@RequestMapping(value = "worksadd/{id}", method = RequestMethod.GET)
	public String worksadd(Model model,@PathVariable String id) {
		//根据员工id查询作品库
		String employeeId = id;
		model.addAttribute("employeeId",employeeId);
		return viewName("worksadd");
	}
	@RequestMapping(value = "worksedit/{id}", method = RequestMethod.GET)
	public String worksedit(Model model,@PathVariable String id) {
		Works t = worksService.getById(id);
		model.addAttribute("t",t);
		return viewName("worksedit");
	}
	@RequestMapping(value = "workssave")
	public String workssave(Works t,RedirectAttributes redirectAttr) {
		try{
			t.setWorksId(CodecUtils.getUUID());
			t.setPhoto(this.getphoto(t.getPhoto()));
			t.setCompId(this.getClientUser().getUser().getCompId());
			t.setStatedate(new Date());
			worksService.save(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("添加失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl("management/employee/worksmanagement/"+t.getEmployeeId());
	}
	@RequestMapping(value = "worksupdate")
	public String worksupdate(Works t,RedirectAttributes redirectAttr) {
		try{
			t.setPhoto(this.getphoto(t.getPhoto()));
			worksService.update(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("修改成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("修改失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("修改失败"));
		}
		return redirectToUrl("management/employee/worksmanagement/"+t.getEmployeeId());
	}
	@ResponseBody
	@RequestMapping(value = "worksdelete/{id}", method = RequestMethod.POST)
	public ResultMsg worksdelete(@PathVariable String id) {
		try{
			//删除之前先去删File中删除照片
			Works w = worksService.getById(id);
			this.delphoto(w.getPhoto());
			worksService.delete(id);
			return ResultMsgFactory.success("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			log.error("删除失败：{}",e.getMessage());
			return ResultMsgFactory.success("删除失败");
		}
	}
	@ResponseBody
	@RequestMapping(value = "holidaydelete/{id}", method = RequestMethod.POST)
	public ResultMsg holidaydelete(@PathVariable String id) {
		try{
			holidayService.delete(id);
			return ResultMsgFactory.success("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			log.error("删除失败：{}",e.getMessage());
			return ResultMsgFactory.success("删除失败");
		}
	}
	
	@RequestMapping(value = "holidaysave")
	public String holidaysave(Holiday t,RedirectAttributes redirectAttr) {
		try{
			t.setHolidayId(CodecUtils.getUUID());
			t.setStatedate(new Date());
			holidayService.save(t);
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.success("添加成功"));
		}catch(Exception e){
			e.printStackTrace();
			log.error("添加失败：{}",e.getMessage());
			redirectAttr.addFlashAttribute("resultMsg",
					ResultMsgFactory.fail("添加失败"));
		}
		return redirectToUrl("management/employee/holidaymanagement/"+t.getEmployeeId());
	}
	@RequestMapping("appointmentindex")
	public String appointmentindex(Model model) {
		return viewName("appointmentindex");
	}
	
	@ResponseBody
	@RequestMapping("appointmentpage")
	public Page<Appointment> appointmentpage(Appointment t,Querier querier) {
		Page<Appointment> page = appointmentService.queryByPage(querier, t);
		return page;
	}
	
	@ResponseBody
	@RequestMapping(value = "delete/{id}", method = RequestMethod.POST)
	public ResultMsg delete(@PathVariable String id) {
		try{
			List<OrderSchedule> scheList = this.scheService.getUnServiceByEmployeeId(id);
			if(scheList!=null&&scheList.size()>0){
				return ResultMsgFactory.success("该员工参与的订单未完成,暂不能删除");
			}
			List<Appointment> appointList = this.appointmentService.getSuccessByEmployee(id);
			if(appointList!=null&&appointList.size()>0){
				return ResultMsgFactory.success("该员工还有未完成的预约,暂不能删除");
			}
			this.getEmployeeService().delete(id);
			return ResultMsgFactory.success("删除成功");
		}catch(Exception e){
			e.printStackTrace();
			log.error("删除失败：{}",e.getMessage());
			return ResultMsgFactory.success("删除失败");
		}
	}
}
